/*
 *  * Copyright (C) 2021 Huawei Device Co., Ltd.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package com.inqbarna.tablefixheaders.utils;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;
import java.util.Optional;

public class ResUtils {

    private final static String TAG = ResUtils.class.getSimpleName();

    public static final int ANGLE_0 = 0;

    public static final int ANGLE_180 = 180;

    public static final int ANGLE_270 = 270;

    public static final int ANGLE_90 = 90;

    public static ShapeElement getShapeElement(Context context, int angle, int... colorIds) {
        ShapeElement element = null;
        if (colorIds != null) {
            RgbColor colors[] = new RgbColor[colorIds.length];
            for (int i = 0; i < colorIds.length; i++) {
                colors[i] = RgbColor.fromArgbInt(getColor(context, colorIds[i]));
            }
            element = getShapeElement(angle, colors);
        }
        return element;
    }

    public static ShapeElement getShapeElement(int angle, RgbColor... colors) {
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setShape(ShapeElement.RECTANGLE);
        shapeElement.setRgbColors(colors);
        switch (angle) {
            case ANGLE_0:
                shapeElement.setOrientation(ShapeElement.Orientation.LEFT_TO_RIGHT);
                break;
            case ANGLE_90:
                shapeElement.setOrientation(ShapeElement.Orientation.BOTTOM_TO_TOP);
                break;
            case ANGLE_180:
                shapeElement.setOrientation(ShapeElement.Orientation.RIGHT_TO_LEFT);
                break;
            case ANGLE_270:
                shapeElement.setOrientation(ShapeElement.Orientation.TOP_TO_BOTTOM);
                break;
            default:
                shapeElement.setOrientation(ShapeElement.Orientation.START_TO_END);
                break;
        }

        return shapeElement;
    }

    public static int getColor(Context context, int id) {
        int result = 0;
        if (context == null) {
            LogUtil.error(TAG, "getColor -> get null context");
            return result;
        }
        ResourceManager manager = context.getResourceManager();
        if (manager == null) {
            LogUtil.error(TAG, "getColor -> get null ResourceManager");
            return result;
        }
        try {
            result = manager.getElement(id).getColor();
        } catch (IOException e) {
            LogUtil.error(TAG, "getColor -> IOException");
        } catch (NotExistException e) {
            LogUtil.error(TAG, "getColor -> NotExistException");
        } catch (WrongTypeException e) {
            LogUtil.error(TAG, "getColor -> WrongTypeException");
        }
        return result;
    }

    public static float getDimen(Context context, int id) {
        float result = 0;
        if (context == null) {
            return result;
        }
        ResourceManager manager = context.getResourceManager();
        if (manager == null) {
            return result;
        }
        try {
            result = manager.getElement(id).getFloat();
        } catch (IOException e) {
        } catch (NotExistException e) {
        } catch (WrongTypeException e) {
        }
        return result;
    }

    /*
     * Returns the device density Pixel
     *
     * @return density pixel value
     */
    public static float getDensityPixel(Context context) {
        Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(context);
        return display.get().getAttributes().densityPixels;
    }
}
